<?php

namespace app\admin\validate\shop;

use think\Validate;

class Setting extends Validate
{
    protected $failException = true;

    protected $rule = [
        'name' => 'require|uniqueField',
    ];

    /**
     * 验证提示信息
     * @var array
     */
    protected $message = [
        'name.require'     => '变量名不能为空',
        'name.uniqueField' => '变量名已存在',
    ];

    /**
     * 字段描述
     */
    protected $field = [
    ];

    /**
     * 验证场景
     */
    protected $scene = [
        'add'  => ['name'],
        'edit' => ['name'],
    ];

    /**
     * 校验唯一
     * @param       $value
     * @param       $rule
     * @param array $data
     * @return bool
     */
    public function uniqueField($value, $rule, array $data = []): bool
    {
        $model = new \app\admin\model\shop\Setting();

        $count = $model->where(['name' => $value])->count();

        return $count < 1;
    }
}